﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using SGS.Logics.Data;
using System.Diagnostics;
using System.IO;
using SGS.IO;
using System.Windows.Media;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;

namespace SGS.Logics.Core
{
    public static class Helper
    {
        public static Random Rand = new Random();

        public static Bitmap GetImageInImage(Bitmap bitmap, Rectangle rect)
        {
            return bitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        public static void Sleep(int milliSeconds)
        {
            System.Threading.Thread.Sleep(milliSeconds);
        }

        public static System.Drawing.Point CursorPosition
        {
            get
            {
                System.Drawing.Point point = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
                return point;
            }
        }

        public static string GetChinesePrice(int price)
        {
            const int Unit = 10000;
            string ret = "0";

            if (price > 0)
            {
                int a, b, c;
                a = price / Unit / Unit;
                b = price / Unit % Unit;
                c = price % Unit;
                ret = "";
                if (a > 0)
                    ret += a.ToString() + "亿";
                if (b > 0)
                    ret += b.ToString() + "万";
                if (c > 0)
                    ret += c.ToString();
            }

            ret += "金幣";
            return ret;
        }

        public static ImageSource GetImageSource(Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                return bitmapImage;
            }
        }

        public static void Log(string text, bool allowDuplicated = false)
        {
            //if (allowDuplicated == true || allowDuplicated == false && Workspace.Instance.StateLogData.LastElement != text)
            //{
            //    Workspace.Instance.StateLogData.Children.Add(text);
            //    if (Workspace.Instance.StateLogData.Children.Count > 100)
            //        Workspace.Instance.StateLogData.Children.RemoveAt(0);

            //    IOManager.Instance.Log(text);
            //}
        }

        public static MainWindow GetMainWindow()
        {
            return (Application.Current.MainWindow as MainWindow);
        }
    }
}
